package com.jyxd.web.controller.patient;

import com.jyxd.web.data.patient.PatientControl;
import com.jyxd.web.service.patient.PatientControlService;
import com.jyxd.web.util.HttpCode;
import com.jyxd.web.util.UUIDUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

/**
 * 质控指标表
 */
@Api(tags = "患者关联质控指标相关接口")
@Controller
@RequestMapping(value = "/patientControl")
public class PatientControlController {

    private static Logger logger= LoggerFactory.getLogger(PatientLogController.class);

    @Autowired
    private PatientControlService patientControlService;

    /**
     * 增加一条质控指标表记录
     * @return
     */
    @ApiOperation("增加一条质控指标表记录")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ResponseBody
    public String insert(@RequestBody PatientControl patientControl){
        JSONObject json=new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data",new ArrayList<>());
        json.put("msg","系统开小差了，请稍后再试。");
        patientControl.setId(UUIDUtil.getUUID());
        patientControl.setCreateTime(new Date());
        patientControlService.insert(patientControl);
        json.put("code",HttpCode.OK_CODE.getCode());
        json.put("msg","添加成功");
        return json.toString();
    }

    /**
     * 更新病质控指标表记录
     * @param patientControl
     * @return
     */
    @ApiOperation("通过患者id更新质控指标数据")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public String update(@RequestBody(required=false) PatientControl patientControl){
        JSONObject json=new JSONObject();
        json.put("code",HttpCode.FAILURE_CODE.getCode());
        json.put("msg","系统开小差了，请稍后再试。");
        if(patientControl!=null && patientControl.getPatientId()!="" && patientControl.getPatientId()!=null ){
            PatientControl patientControlNew=patientControlService.queryData(patientControl.getPatientId());
            if(patientControlNew!=null){
                patientControlNew.setDvtFlag(patientControl.getDvtFlag());
                patientControlNew.setVapFlag(patientControl.getVapFlag());
                patientControlNew.setBunble3hFlag(patientControl.getBunble3hFlag());
                patientControlNew.setBunble6hFlag(patientControl.getBunble6hFlag());
                patientControlNew.setBak1(patientControl.getBak1());
                patientControlNew.setBak2(patientControl.getBak2());
                patientControlNew.setBak3(patientControl.getBak3());
                patientControlNew.setBak4(patientControl.getBak4());
                patientControlNew.setBak5(patientControl.getBak5());
                patientControlNew.setBak6(patientControl.getBak6());
                patientControlNew.setBak7(patientControl.getBak7());
                patientControlNew.setBak8(patientControl.getBak8());
                patientControlNew.setBak9(patientControl.getBak9());
                patientControlNew.setBak10(patientControl.getBak10());
                patientControlService.update(patientControlNew);
                json.put("msg","更新成功");
            }else{
                json.put("msg","更新失败，没有这个对象。");
                return json.toString();
            }
        }
        json.put("code",HttpCode.OK_CODE.getCode());
        return json.toString();
    }
    /**
     * 通过患者id查询病质控指标表记录
     * @param map
     * @return
     */
    @ApiOperation("通过患者id查询病质控指标表记录")
    @RequestMapping(value = "/getData", method = RequestMethod.POST)
    @ResponseBody
    public String getData(@RequestBody(required=false) Map<String,Object> map){
        JSONObject json=new JSONObject();
        json.put("code",HttpCode.FAILURE_CODE.getCode());
        if(map!=null && map.containsKey("patientId") ){
            PatientControl patientControl=patientControlService.queryData(map.get("patientId").toString());
            if(patientControl!=null){
                json.put("msg","查询成功");
                json.put("data",patientControl);
            }else{
                json.put("msg","查询失败，没有这个对象。");
                return json.toString();
            }
        }
        json.put("code",HttpCode.OK_CODE.getCode());
        return json.toString();
    }
}
